/*-------------------------------------------------------------------------
 - Copyright (c) 2024-2025 [XD-AMCC TEAM]
 - [XD-AMCC] is licensed under Mulan PSL v2.
 - You can use this software according to the terms and conditions of the Mulan PSL v2.
 - You may obtain a copy of Mulan PSL v2 at:
 -          http://license.coscl.org.cn/MulanPSL2
 - THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES
 - OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 - TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 - See the Mulan PSL v2 for more details.
 ------------------------------------------------------------------------*/


/**
 * @brief Implementation of abstract class reference for grammar symbols.
 *
 *
 * @author  WXQ#XDU
 * @date    2024.11
 */

#include <stdlib.h>
#include "../include/amcc_refcount.h"


void ref_init_(t_reference_ptr referencePtr, t_reference_free pFuncFree) {
    if (NULL == referencePtr) return;

    referencePtr->free     = pFuncFree;
    referencePtr->refCount = 1;
}


// helper to pass an attribute object from a node to another
t_reference_ptr ref_copy_(t_reference_ptr referencePtr) {
    if (NULL == referencePtr) return NULL;
    ++(referencePtr->refCount);
    return referencePtr;
}


void ref_free_(t_reference_ptr referencePtr) {
    if (NULL == referencePtr) return;
    -- (referencePtr->refCount);
    if (referencePtr->refCount <= 0 ) {
        if (NULL != referencePtr->free)
            referencePtr->free(referencePtr);
        free(referencePtr);
    }
}
